import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:tv_friso_screen/utils/phone_info_utils.dart';
import 'package:tv_friso_screen/utils/share_pre_utils.dart';
import 'package:tv_friso_screen/welcome_page.dart';

import 'nav_key.dart';

setLanguage() async {
  var language = SharePreUtils.getLanguage();
  if (language.isEmpty) {
    await SharePreUtils.setLanguage("zh");
  }
}

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await SharePreUtils.initSharedPreferences();
  await setLanguage();
  SystemChrome.setEnabledSystemUIMode(SystemUiMode.immersive); //隐藏状态栏

  if (Platform.isAndroid) {
    // 初始化服务绑定
    SystemChrome.setPreferredOrientations(
      [DeviceOrientation.landscapeLeft],
    ) //横屏展示
        .then((_) {
      // 运行应用程序
      runApp(const MyApp());
    });
  } else if (Platform.isIOS) {
    // 初始化服务绑定
    SystemChrome.setPreferredOrientations(
      [DeviceOrientation.landscapeRight],
    ) //横屏展示
        .then((_) {
      // 运行应用程序
      runApp(const MyApp());
    });
  } else {
    // 初始化服务绑定
    SystemChrome.setPreferredOrientations(
      [DeviceOrientation.landscapeLeft],
    ) //横屏展示
        .then((_) {
      // 运行应用程序
      runApp(const MyApp());
    });
  }
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return MainViewState();
  }
}

class MainViewState extends State<MyApp> with WidgetsBindingObserver {
  static const MaterialColor blue17439A = MaterialColor(
    0xFF17439A,
    <int, Color>{
      50: Color(0xFFD61727),
      100: Color(0xFFD61727),
      200: Color(0xFFD61727),
      300: Color(0xFFD61727),
      400: Color(0xFFD61727),
      500: Color(0xFFD61727),
      600: Color(0xFFD61727),
      700: Color(0xFFD61727),
      800: Color(0xFFD61727),
      900: Color(0xFFD61727),
    },
  );

  @override
  void initState() {
    super.initState();

    ///获取应用信息
    PhoneInfoUtils.initPackageInfo();

    ///获取设备信息
    PhoneInfoUtils.initDeviceInfo();
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  void dispose() {
    // 移除观察者
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    // 监控应用生命周期状态
    if (state == AppLifecycleState.resumed) {
      // 应用重新打开时的逻辑
      if (Platform.isAndroid) {
        // 初始化服务绑定
        SystemChrome.setPreferredOrientations(
          [DeviceOrientation.landscapeLeft],
        );
      } else if (Platform.isIOS) {
        // 初始化服务绑定
        SystemChrome.setPreferredOrientations(
          [DeviceOrientation.landscapeRight],
        );
      } else {
        // 初始化服务绑定
        SystemChrome.setPreferredOrientations(
          [DeviceOrientation.landscapeLeft],
        );
      }
    } else if (state == AppLifecycleState.paused) {
      // 应用被暂停
    } else if (state == AppLifecycleState.detached) {
      // 应用被分离
    }
  }

  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: const Size(1920, 1080),
      builder: (BuildContext context, Widget? child) {
        return MaterialApp(
          navigatorKey: NavKey.navKey,
          debugShowCheckedModeBanner: false,
          title: '驾驶舱大屏',

          theme: ThemeData(
            primarySwatch: blue17439A,
            fontFamily: 'PingFang',
            scaffoldBackgroundColor: Colors.transparent,
          ),
          home: const WelComePage(), //启动页
          // home: Center(
          //   child: Container(
          //     width: 800.w,
          //     height: 600.w,
          //     color: Colors.amberAccent,
          //     child: const SmoothLineChart(),
          //   ),
          // ),

          builder: EasyLoading.init(),
        );
      },
    );
  }
}
